//
//  MovieListSameViewController.m
//  UI_豆瓣项目
//
//  Created by dllo on 15/11/4.
//  Copyright (c) 2015年 关业达. All rights reserved.
//

#import "MovieListSameViewController.h"
#import "MovieListSameCollectionViewCell.h"
#import "AppTools.h"
#import "MovieDataViewController.h"
@interface MovieListSameViewController ()<UICollectionViewDataSource, UICollectionViewDelegate>

@property (nonatomic, retain)NSMutableArray *myArray;
@property (nonatomic, retain)UICollectionView *myCollectionView;


@end

@implementation MovieListSameViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"电影";
    
    self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"btn_nav_list@2x.png"] style:UIBarButtonItemStylePlain target:self action:@selector(rightAction:)] autorelease];
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.itemSize = CGSizeMake(90, 120);
    // 最小行间距
    flowLayout.minimumLineSpacing = 60;
    // 最小列间距
    flowLayout.minimumInteritemSpacing = 20;
    // 设置与四周边界距离
    flowLayout.sectionInset = UIEdgeInsetsMake(20, 30, 35, 30);
    
    self.myCollectionView = [[UICollectionView alloc] initWithFrame:[[UIScreen mainScreen] bounds] collectionViewLayout:flowLayout];
    [self.view addSubview:self.myCollectionView];
    [_myCollectionView release];
    
    self.myCollectionView.delegate = self;
    self.myCollectionView.dataSource = self;
    self.myArray = [NSMutableArray array];
    
    // 注册cell
    [self.myCollectionView registerClass:[MovieListSameCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];// 改cell
    
    /*
    // 请求
    NSString *urlStr = @"http://project.lanou3g.com/teacher/yihuiyun/lanouproject/movielist.php";
    // 解析
    [AppTools geturlStr:urlStr Block:^(id result) {
        NSMutableDictionary *dic = result;
        NSString *reason = [dic objectForKey:@"reason"];
        NSMutableArray *array = [dic objectForKey:@"result"];

    if ([reason isEqualToString:@"success"]){
        for (NSMutableDictionary *dic in array) {
            Movie *movie = [[Movie alloc] init];
            [movie setValuesForKeysWithDictionary:dic];
            [self.myArray addObject:movie];
            [movie release];
        }
        [self.myCollectionView reloadData];
        NSLog(@"======%@", self.myArray);
    }
         [self.myCollectionView reloadData];
}];
     
     */
    // 请求数据
    NSString *urlStr = @"http://project.lanou3g.com/teacher/yihuiyun/lanouproject/movielist.php";
    
//    //菊花
//    self.hub = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
//    [self.hub show:YES];
//    self.hub.labelText = @"别TM着急!!!";
    
    [AppTools geturlStr:urlStr Block:^(id result) {
        
        NSMutableDictionary *dic = result;
        NSString *error_code = [dic objectForKey:@"error_code"];
        NSString *reason = [dic objectForKey:@"reason"];
        NSMutableArray *array = [dic objectForKey:@"result"];
        if ([reason isEqualToString:@"success"]) {
            
            
//            // 删除表
//            [[MovieListSingleton shareMovieList] dropTable];
//            
//            // 创建表
//            [[MovieListSingleton shareMovieList] createTable];
            
            for (NSDictionary *dic in array) {
                Movie *movie = [[Movie alloc] init];
                [movie setValuesForKeysWithDictionary:dic];
                [self.myArray addObject:movie];
//                // 插入表
//                [[MovieListSingleton shareMovieList] insertMovie:movie];
                
                [movie release];
                
                // 刷新
                [self.myCollectionView reloadData];
//                //停菊花
//                [self.hub hide:YES];
                NSLog(@"arrayhehehe = %@", self.myArray);
            }
            
        }else{
            NSLog(@"请求失败");
        }
    }];
    
    
    
}

// 设置显示item个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.myArray.count;
//    return 10;
    
    
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    // 注意更改自定义cell
    MovieListSameCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath]; // 改cell
    cell.backgroundColor = [UIColor colorWithRed:arc4random()%256/255.0 green:arc4random()%256/255.0 blue:arc4random()%256/255.0 alpha:1.0];
    //赋值
    cell.myMovie = [self.myArray objectAtIndex:indexPath.item];
    
    NSLog(@"%@", cell.myMovie.movieName);
    return cell;
}



- (void)rightAction:(UIBarButtonItem *)barbutn
{
    [self.navigationController popToRootViewControllerAnimated:YES];
}

//点击触发方法
- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath
{
    MovieDataViewController *detailVC = [[MovieDataViewController alloc] init];
    Movie *movie = [self.myArray objectAtIndex:indexPath.item];
    detailVC.movieID = movie.movieId;
    [self.navigationController pushViewController:detailVC animated:YES];
    [detailVC release];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
